﻿Imports System.Management.Automation
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

<Cmdlet(VerbsCommon.Add, TeamSystemNouns.WorkItemQuery, ConfirmImpact:=ConfirmImpact.Low, SupportsShouldProcess:=True)> _
Public Class AddWorkItemQueryCmdlet
    Inherits TeamProjectCmdletBase

    Public Sub New()
    End Sub

    Private _name As String

    <Parameter(Position:=2, Mandatory:=True, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _query As String

    <Parameter(Position:=3, Mandatory:=True, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Query() As String
        Get
            Return _query
        End Get
        Set(ByVal value As String)
            _query = value
        End Set
    End Property

    Private _description As String

    <Parameter(Position:=4, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    Private _public As SwitchParameter

    <Parameter(ValueFromPipelineByPropertyName:=True)> _
    Public Property [Public]() As SwitchParameter
        Get
            Return _public
        End Get
        Set(ByVal value As SwitchParameter)
            _public = value
        End Set
    End Property

    Private _passThru As SwitchParameter

    <Parameter()> _
    Public Property PassThru() As SwitchParameter
        Get
            Return _passThru
        End Get
        Set(ByVal value As SwitchParameter)
            _passThru = value
        End Set
    End Property

    Protected Overrides Sub ProcessRecord()
        Dim scope As QueryScope
        If [Public].IsPresent Then
            scope = QueryScope.Public
        Else
            scope = QueryScope.Private
        End If

        Dim storedQuery As New StoredQuery(scope, Name, Query, Description)

        If ShouldProcess(Name) Then
            workItemStore.Projects(TeamProject).StoredQueries.Add(storedQuery)
        End If

        If PassThru.IsPresent Then
            WriteObject(storedQuery)
        End If
    End Sub
End Class